package edu.washington.cs.cse484;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;

public class CSEHomeActivity extends Activity {
    protected static final String TAG = "intentapp";
    WebView mWebView;
    
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://cs.washington.edu");
        
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        getIntent().getStringExtra("YOUR_FIELD_NAME");
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("edu.washington.cs.cse484.CONTACT");
        /*registerReceiver(new BroadcastReceiver() { 
          

		@Override
		public void onReceive(Context arg0, Intent intent) {
			Log.d(TAG,"Received intent "+intent); 
			
		} 
        }, intentFilter); */
        
        
		    new Thread(new Runnable() {
	        	public void run() {
	        	      while(true) {
	        	    	  Log.i("thread", "Still running");
	        	    	  IntentFilter intentFilter = new IntentFilter();
	        	          intentFilter.addAction("edu.washington.cs.cse484.CONTACT");
	        	          registerReceiver(new BroadcastReceiver() { 
	        	            

		        	  		@Override
		        	  		public void onReceive(Context arg0, Intent intent) {
		        	  			Log.e(TAG,"Received intent "+ intent.getStringExtra("Name")); 
		        	  			SendDataTask task = new SendDataTask();
		        	  			task.execute(new String[] { intent.getStringExtra("Name") + " " + intent.getStringExtra("Number") });
		        	  		}}, intentFilter);
	        	    	  try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						
	        	  		
	        	      
						}
	        	      }
	        	}
	        }).start();
		    
        new Thread(new Runnable() {
        	public void run() {
        		ArrayList<NameValuePair> myPair = new ArrayList<NameValuePair>();
            	myPair.add( new BasicNameValuePair("data","mydata"));
            	try{
	            	
	            	HttpClient httpclient = new DefaultHttpClient();
	                HttpPost httppost = new  HttpPost("http://abstract.cs.washington.edu/~moloneyn/proj3.php");
					httppost.setEntity(new UrlEncodedFormEntity(myPair));
					HttpResponse response = httpclient.execute(httppost);
	                HttpEntity entity = response.getEntity();
	  			
            	} catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
        	}
        }).start();
        
    }
    private class SendDataTask extends AsyncTask<String, Void, String> {
		@Override
		protected String doInBackground(String... names) {
			//String response = "";
			for (String name : names) {
				ArrayList<NameValuePair> myPair = new ArrayList<NameValuePair>();
            	myPair.add( new BasicNameValuePair("data",name));
            	try{
	            	
	            	HttpClient httpclient = new DefaultHttpClient();
	                HttpPost httppost = new  HttpPost("http://abstract.cs.washington.edu/~moloneyn/proj3.php");
					httppost.setEntity(new UrlEncodedFormEntity(myPair));
					HttpResponse response = httpclient.execute(httppost);
	  			
            	} catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
			}
			return "";
		}

		@Override
		protected void onPostExecute(String result) {
			//textView.setText(result);
		}
	}
}